
var that, timer,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  qrcode = require('../../utils/qrcode.js'),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabindex: 1,
    swiperlist: [
      { src: 'https://czl.sskj.公司/src/jiayouzhan_index_swiper.png', id: 1 },
      { src: 'https://czl.sskj.公司/src/jiayouzhan_index_swiper.png', id: 1 },
      { src: 'https://czl.sskj.公司/src/jiayouzhan_index_swiper.png', id: 1 },
    ],
    list: [
      // {
      //   state:'YSQ',
      //   ghname:'xx',
      //   headimg:'https://czl.sskj.公司/src/jiayouzhan_shop_headimg.jpg',
      //   avatar:[
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar1.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar2.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar3.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar4.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar2.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar1.png',
      //   ],
      //   title:'西数泡芙',
      //   tuangouprice:12,
      //   marketprice:16,

      // },
      // {
      //   state:'YXJ',
      //   ghname:'xx',
      //   headimg:'https://czl.sskj.公司/src/jiayouzhan_shop_headimg.jpg',
      //   avatar:[
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar1.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar2.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar3.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar4.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar2.png',
      //     'https://czl.sskj.公司/src/jiayouzhan_shop_avatar1.png',
      //   ],
      //   title:'西数泡芙',
      //   tuangouprice:12,
      //   marketprice:16,
      //   yg:244,
      // },
    ],
    page: 1,
    istop: 2,
    corpBulkTag: 0,
    classiflist: [],
    hotlist: [],
    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    if (app.logininfo) {
      that.getdata();
    } else {
      util.setwatcher(app, this.afterlogin)
    }
  },
  afterlogin: {
    logininfo: res => {
      that.getdata();
      // that.getCoupon()
    }
  },
  getdata() {
    wx.showLoading({
      title: '加载中',
    })
    let { classiflist, hotlist } = that.data
    api.getcatelist(res => {
      if (res.data.code == 0) {
        console.log(res.data.entity, 'entityentityentityentity');
        classiflist = res.data.entity.data
        that.setData({ classiflist })
      }
    })
    api.gethotset(res => {
      if (res.data.code == 0) {
        hotlist = res.data.entity.data
        that.setData({ hotlist })
      }
    })
    // api.getbulkList({ lid: tabindex, page }, res => {
    //   wx.hideLoading()
    //   if (res.data.code == 0) {
    //     let { infoArr, nextPage } = res.data.entity.data
    //     if (infoArr.length > 0) {
    //       infoArr.forEach(x => {
    //         list.push(x)
    //       })
    //     }
    //     that.setData({ list, nextPage })
    //   }
    // })
    let posId = 2
    api.getfocus(posId, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        let { infoArr } = res.data.entity.data
        console.log(infoArr, 'infoArrinfoArrinfoArr');
        that.setData({ infoArr })
        console.log(res.data, 'hhgjhghjghgfgf');
      }
    })
    that.getlist()
  },
  getlist() {
    wx.showLoading({
      title: '加载中',
    })
    let { list, page } = that.data
    api.getallgoods(page, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        let { infoArr, nextPage } = res.data.entity.data
        if (infoArr.length > 0) {
          infoArr.forEach(x => {
            list.push(x)
          })
        }
        that.setData({ list, nextPage })
      }
    })
  },
  switch(e) {
    let { tabindex } = this.data, { i } = e.currentTarget.dataset;
    if (i != tabindex) {
      that.setData({ tabindex: i, list: [], page: 1 }, () => {
        that.getdata()
      })
    }
  },
  todetail(e) {
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../shopdetail/shopdetail?id=' + id,
    })
  },
  toclass(e) {
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../classification/classification?cid=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let { corpBulkTag } = app.logininfo, { istop } = that.data
    that.setData({ corpBulkTag })
    if (corpBulkTag == 0) {
      wx.showModal({
        title: '提示',
        content: '团购模块暂未开通，敬请期待！',
        showCancel: !1,
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../index/index',
            })
          }
        }
      })
    }
    if (istop == 2) {
      console.log(111111);
      wx.pageScrollTo({
        scrollTop: 0
      });
    }
    if (that.data.navbarInitTop == 0) {
      //获取节点距离顶部的距离
      wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var navbarInitTop = parseInt(rect.top);
          that.setData({
            navbarInitTop: navbarInitTop
          });
        }
      }).exec();

    }
  },
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度

    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false;
    }
    that.setData({
      isFixedTop: isSatisfy
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    that.setData({ istop: 2})
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    that.setData({ istop: 2 })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    that.setData({ list: [], page: 1 }, () => {
      that.getdata();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { nextPage, page } = that.data
    if (nextPage == 1) {
      page = page + 1
      that.setData({ page }, () => {
        that.getlist()
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})